import requests,json,re,time,random
from mysqlhelper import MysqlHelper

helper = MysqlHelper()
headers = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
    'Referer': 'https://m.weibo.cn/?jumpfrom=weibocom',
}

lid = ['1225419417','1217677557','1624395410','1151243311','1630916460','1254512737','1641545285','1195031270','1652482560']

def zhuye(uid):
    url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_followers_-_' + uid + '_-_1042015:tagCategory_020&luicode=10000011&lfid=100505' + uid
    print(uid)
    response = requests.get(url)
    res_dict = response.json()['data']['cards']
    # print(res_dict)
    try:
        for i in res_dict:
            # print(type(i),i)
            for k in i['card_group']:
                # print(type(k),'k',k)
                if 'user' in k:
                    uid = k['user']['id']
                    # if len(lid) < 1000:
                    lid.insert(0,str(uid))
                    print('列表长度',len(lid))
                    shuju(lid.pop())

    except:
        if lid:
            shuju(lid.pop())
        else:
            uid = random.randrange(1111111111, 9999999999)
            zhuye(str(uid))

def shuju(uid):
    # url = 'https://m.weibo.cn/u/'+uid
    try:
        # print('shuju',uid)
        url_x = 'https://m.weibo.cn/api/container/getIndex?type=uid&value='+uid+'&containerid=107603'+uid+'&page={}'

        for i in range(1,11):
            # 拼装url的详细分页
            url = url_x.format(i)
            print('执行到了第{}页'.format(i),uid)

            # 调用requests
            response = requests.get(url)
            # print(response)

            # 获取具体需要的信息
            res_dict = response.json()
            # print(res_dict)
            cards = res_dict['data']['cards']
            # print(cards)
            # time.sleep(1)
            for card in cards:
                if 'mblog' in card:
                    # 微博名
                    user = card['mblog']['user']['screen_name']
                    # print(user)
                    # 微博内容
                    text = card['mblog']['text']
                    # print(texts)
                    res = re.findall('[0-9a-zA-Z><="_&:%/.?#-【]',text)
                    for i in res:
                        if i in text:
                            # print(i)
                            text = text.replace(i, '').strip()
                    # print(text)
                    # 时间
                    times = card['mblog']['created_at']
                    # print(times)
                    # 转发数量
                    zhuanfa = card['mblog']['reposts_count']
                    # print(zhuanfa)
                    # 评价数量
                    pingjia = card['mblog']['comments_count']
                    # print(pingjia)
                    # 点赞数量
                    dianzan = card['mblog']['attitudes_count']
                    # print(dianzan)

                    insert_sql = 'INSERT INTO weibo(user,text,times,zhuanfa,pingjia,dianzan)VALUES (%s, %s,%s, %s,%s, %s)'
                    data = (user,text,times,zhuanfa,pingjia,dianzan)
                    helper.execute_insert_sql(insert_sql, data)

    except:
        shuju(lid.pop())
    zhuye(uid)


zhuye(lid.pop())
